package com.ztgf.logistics.api.cms.deppon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.common.vo.UserVO;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserInfoService;
import com.ztgf.logistics.models.vo.deppon.LogisticsUserAuditVO;
import com.ztgf.logistics.models.vo.deppon.LogisticsUserVO;
import com.ztgf.order.models.service.pay.IPayLogisticsService;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.sys.models.service.ISessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserService;
import com.ztgf.logistics.models.bo.deppon.LogisticsUserSaveBO;
import com.ztgf.logistics.models.bo.deppon.LogisticsUserBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsUserDTO;

/**
 * 物流用户信息表控制器
 *
 * @author mxli
 * @Date 2021-09-13 22:16:41
 */

@RestController
@RequestMapping("/cms/logisticsUser")
@Slf4j
@Api(value = "LogisticsUserController",tags = "物流用户信息表接口")
public class LogisticsUserController {

    @DubboReference
    private ILogisticsUserService logisticsUserService;
    @DubboReference
    private IPayLogisticsService payLogisticsService;
    @DubboReference
    private ISessionService sessionService;
    @DubboReference
    private ILogisticsUserInfoService logisticsUserInfoService;
    @DubboReference
    private IAuditService iAuditService;

    @ApiOperation(value = "修改物流保证金",notes = "修改物流保证金")
    @PostMapping(value = "/updateMguaranteeMoneyQuota")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId",value = "店铺id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "money",value = "保证金金额",required = true,paramType = "query", dataType = "BigDecimal")
    })
    public Result updateMguaranteeMoneyQuota(Long shopId, BigDecimal money) {
        UserVO userVO = sessionService.getUserVO();
        return payLogisticsService.updateMguaranteeMoneyQuota(shopId, money, userVO.getId(), userVO.getName());
    }

    @ApiOperation(value = "修改物流信用金",notes = "修改物流信用金")
    @PostMapping(value = "/updateLogisticsMoneyQuota")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId",value = "店铺id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "money",value = "信用金金额",required = true,paramType = "query", dataType = "BigDecimal")
    })
    public Result updateLogisticsMoneyQuota(Long shopId, BigDecimal money) {
        UserVO userVO = sessionService.getUserVO();
        return logisticsUserService.updateLogisticsMoneyQuota(null ,shopId, money, userVO.getId(), userVO.getName());
    }

    @ApiOperation(value = "解冻物流保证",notes = "解冻物流保证")
    @PostMapping(value = "/saveLogisticsUnfreezeMarginMoney")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId",value = "店铺id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "money",value = "保证金金额",required = true,paramType = "query", dataType = "BigDecimal")
    })
    public Result saveLogisticsUnfreezeMarginMoney(Long shopId, BigDecimal money) {
        UserVO userVO = sessionService.getUserVO();
        payLogisticsService.saveLogisticsUnfreezeMarginMoney(shopId, money, userVO.getId(), userVO.getName());
        return new Result();
    }

    @ApiOperation(value = "扣除物流保证",notes = "扣除物流保证")
    @PostMapping(value = "/saveLogisticsDeductMargin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId",value = "店铺id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "money",value = "保证金金额",required = true,paramType = "query", dataType = "BigDecimal")
            ,@ApiImplicitParam(name = "notes",value = "说明",required = true,paramType = "query", dataType = "String")
    })
    public Result saveLogisticsDeductMargin(Long shopId, BigDecimal money, String notes) {
        UserVO userVO = sessionService.getUserVO();
        payLogisticsService.saveLogisticsDeductMargin(shopId, money, notes, userVO.getId(), userVO.getName());
        return new Result();
    }

    @ApiOperation(value = "修改现结用户",notes = "修改现结用户")
    @PostMapping(value = "/updateType")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    })
    public Result updateType(Long id) {
        LogisticsUserDTO dto = new LogisticsUserDTO();
        dto.setId(id);
        dto.setType(0);
        dto.setContractStatus(0);
        dto.setFlag(1);
        boolean flag = logisticsUserService.updateById(dto);
        if(!flag){
            return new Result(CodeEnum.FAIL_100008);
        } else {
            dto = logisticsUserService.selectById(id);
            AuditDTO auditDTO = new AuditDTO();
            auditDTO.setId(dto.getAuditId());
            auditDTO.setStatus(3);
            iAuditService.updateById(auditDTO);
            return new Result();
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<LogisticsUserAuditVO> get(@RequestParam("id") Long id){
        return logisticsUserInfoService.get(id);
    }

    @ApiOperation(value = "物流用户信息表列表",notes = "物流用户信息表列表")
    @PostMapping("/queryPageList")
    public Result<Page<LogisticsUserVO>> queryPageList(LogisticsUserBO bo){
        return logisticsUserService.queryPageList(bo);
    }
}
